package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.pi.common.CodesConstants;
import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.exception.ApplicationException;
import com.pi.framework.core.model.IEditable;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.TwaDocAsnDetails;
import com.pi.wms.entity.TwaDocAsnHeader;
import com.pi.wms.entity.TwtActPalletizeList;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Slf4j
public abstract class BaseAsnController<T, ID extends Serializable> extends BaseCrudController<T, ID>{


    @Autowired
    private AsnService asnService;

    @Autowired
    protected AsnDetailsService detailsService;

    @Autowired
    protected PalletizeService palletizeService;

    @Autowired
    protected TaskService taskService;

    @Autowired
    protected WasCustomWorkService customWorkService;

    @Override
    protected void preSave(T m, HttpServletRequest request) {
        super.preSave(m, request);
        String msg = this.preSaveCheck(m);
        if (!StringUtils.isNullOrBlank(msg)){
            throw new ApplicationException(msg);
        }
        msg = this.afterSavePopulate(m, SecurityUtils.getLoginUsername());
    }

    protected String preSaveCheck(T header){
        return "";
    }

    protected String afterSavePopulate(T header, String worker){
        return "";
    }

    protected String close(Long id, boolean palletizeOpen, boolean palletizeAutoMode){
        String msg="";
        if (id==null){
            msg="无效的单证ID";
            return msg;
        }
        TwaDocAsnHeader h = this.asnService.findById(id);
        if (h==null){
            msg="找不到单证,请确认是否已被其他用户删除.单证ID="+String.valueOf(id);
            return msg;
        }
        if (h != null && h instanceof IEditable && !((IEditable) h).isEditable()) {
            msg="该数据为系统固定不可编辑，请选择其他操作!";
            return msg;
        }
        if (StringUtils.isNullOrBlank(h.getStatus())){
            msg="无效的单证状态,请联系系统管理员.单证号="+h.getDocumentNumber();
            return msg;
        }
        if (h.getStatus().equals(WmsConstants.ASN_STATUS_CLOSE)){
            msg="单证已经关闭.单证号="+h.getDocumentNumber();
            return msg;
        }
        if (h.getStatus().equals(WmsConstants.ASN_STATUS_CANCELL)){
            msg="单证已经取消.单证号="+h.getDocumentNumber();
            return msg;
        }
        List<TwaDocAsnDetails> details=null;
        try{
            details=detailsService.findByDocumentId(id);
            for (TwaDocAsnDetails d:details){
                if (d==null){
                    continue;
                }
                if (StringUtils.isNullOrBlank(d.getLineStatus())){
                    continue;
                }
            }
        }catch(Exception e){
            msg=e.getMessage();
            return msg;
        }

        List<TwtActPalletizeList> pals=null;
        if (palletizeOpen){
            try{
                pals=palletizeService.findByDocumentId(id);
                for (TwtActPalletizeList p:pals){
                    if (p==null){
                        continue;
                    }
                    if (StringUtils.isNullOrBlank(p.getLineStatus())){
                        continue;
                    }
                    if (!palletizeAutoMode){
                        if (!WmsConstants.ASN_STATUS_CLOSE.equals(p.getLineStatus())){
                            msg="存在已已经生成的码盘,请先完成或删除码盘数据.单证号="+h.getDocumentNumber();
                            return msg;
                        }
                    }
                }
            }catch(Exception e){
                msg=e.getMessage();
                return msg;
            }
        }
        //
        List<TwtActTaskList> tasks=taskService.queryWorkingTasks(WmsConstants.TASK_TYPE_PUTAWAY, id);
        if ((tasks!=null) && (tasks.size()>0)){
            msg="存在未完成的上架任务,请先处理.不能关闭.单证号="+h.getDocumentNumber();
            return msg;
        }
        //
        if (palletizeOpen){
            try{
                if ((pals!=null) && (pals.size()>0)){
                    this.palletizeService.delete(pals);
                }
            }catch(Exception e){
                msg=e.getMessage();
                return msg;
            }
        }
        //
        try{
            if ((details!=null) && (details.size()>0)){

                for (TwaDocAsnDetails d:details){
                    if (d==null){
                        continue;
                    }
                    if (WmsConstants.ASN_STATUS_CLOSE.equals(d.getLineStatus())){
                        continue;
                    }
                    if (WmsConstants.ASN_STATUS_CANCELL.equals(d.getLineStatus())){
                        continue;
                    }
                    d.setLineStatus(WmsConstants.ASN_STATUS_CLOSE);
                    d.setLastModifiedDate(new Date());
                    d.setLastModifiedBy(SecurityUtils.getLoginUsername());
                }
                this.detailsService.update(details);
            }
        }catch(Exception e){
            msg=e.getMessage();
            return msg;
        }
        //
        try{
            h.setStatus(WmsConstants.ASN_STATUS_CLOSE);
            h.setLastModifiedDate(new Date());
            h.setLastModifiedBy(SecurityUtils.getLoginUsername());
            asnService.update(h);
        }catch(Exception e){
            msg=e.getMessage();
        }
        return msg;
    }

    protected String delete(Long id, boolean palletizeOpen, boolean palletizeAutoMode){
        String msg="";
        if (id==null){
            msg="无效的单证ID";
            return msg;
        }
        TwaDocAsnHeader h = this.asnService.findById(id);
        if (h==null){
            msg="找不到单证,请确认是否已被其他用户删除.单证ID="+String.valueOf(id);
            return msg;
        }
        if (h != null && h instanceof IEditable && !((IEditable) h).isEditable()) {
            msg="该数据为系统固定不可编辑，请选择其他操作!";
            return msg;
        }
        if (StringUtils.isNullOrBlank(h.getStatus())){
            msg="无效的单证状态,请联系系统管理员.单证号="+h.getDocumentNumber();
            return msg;
        }
        if (!h.getStatus().equals(WmsConstants.ASN_STATUS_CREATE)){
            msg="非创建状态单证,不能删除.单证号="+h.getDocumentNumber();
            return msg;
        }
        List<TwaDocAsnDetails> details=null;
        try{
            BigDecimal qty=BigDecimal.ZERO;
            details=detailsService.findByDocumentId(id);
            for (TwaDocAsnDetails d:details){
                if (d==null){
                    continue;
                }
                if (StringUtils.isNullOrBlank(d.getLineStatus())){
                    continue;
                }
                if (!WmsConstants.ASN_STATUS_CREATE.equals(d.getLineStatus())){
                    msg="存在已经作业的单证行,不能删除.单证号="+h.getDocumentNumber()+",行号="+d.getLineNumber();
                    return msg;
                }
                /**
                qty=d.getQtyPalletizedEach();
                if ((qty!=null) && (qty.compareTo(BigDecimal.ZERO)>0)){
                    msg="存在已经码盘的单证行,请先清除码盘数据.单证号="+h.getDocumentNumber()+",行号="+d.getLineNumber();
                    return msg;
                }
                 **/
                qty=d.getQtyReceivedEach();
                if ((qty!=null) && (qty.compareTo(BigDecimal.ZERO)>0)){
                    msg="存在已经收货的单证行,不能删除.单证号="+h.getDocumentNumber()+",行号="+d.getLineNumber();
                    return msg;
                }
            }
        }catch(Exception e){
            msg=e.getMessage();
            return msg;
        }
        //
        List<TwtActPalletizeList> pals=null;
        if (palletizeOpen){
            try{
                pals=palletizeService.findByDocumentId(id);
                for (TwtActPalletizeList p:pals){
                    if (p==null){
                        continue;
                    }
                    if (StringUtils.isNullOrBlank(p.getLineStatus())){
                        continue;
                    }
                    if (!palletizeAutoMode){
                        if (!WmsConstants.ASN_STATUS_CLOSE.equals(p.getLineStatus())){
                            msg="存在已已经生成的码盘,请先删除码盘数据.单证号="+h.getDocumentNumber();
                            return msg;
                        }
                    }
                }
                if ((pals!=null) && (pals.size()>0)){
                    this.palletizeService.delete(pals);
                }
            }catch(Exception e){
                msg=e.getMessage();
                return msg;
            }
        }
        //
        try{
            if ((details!=null) && (details.size()>0)){
                this.detailsService.delete(details);
            }
        }catch(Exception e){
            msg=e.getMessage();
            return msg;
        }
        //
        try{
            asnService.delete(h);
        }catch(Exception e){
            msg=e.getMessage();
        }
        return msg;
    }


}
